package com.ymatou.iapi.trading.operate.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.ConfirmReceiveBean;
import com.ymatou.iapi.trading.operate.service.ConfirmReceiveCall;
import com.ymatou.operate.trading.api.parameter.FrozenOrUnFrozenOrderBean;
import com.ymatou.operate.trading.api.service.FrozenOrUnFrozenOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.constant.ECode;

/**
 * 买家确认收货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ConfirmReceive {

	private static ConfirmReceiveBean confirmreceivebean;
	private static ConfirmReceiveCall confirmreceivecall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买家确认收货");
	}

	@Before
	public void caseUp() {

		confirmreceivebean = new ConfirmReceiveBean();
		confirmreceivecall = new ConfirmReceiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(confirmreceivecall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	// 订单投诉维权(冻结和解冻)接口
	private void frozenOrder(boolean isFrozen, Integer orderId, int sellerId) {
		FrozenOrUnFrozenOrderBean frozenorunfrozenorderBean = new FrozenOrUnFrozenOrderBean();
		FrozenOrUnFrozenOrderCall frozenorunfrozenorderCall = new FrozenOrUnFrozenOrderCall();
		frozenorunfrozenorderBean.setOrderId(orderId);
		frozenorunfrozenorderBean.setUserId(sellerId);
		frozenorunfrozenorderBean.setIsFrozen(isFrozen);
		frozenorunfrozenorderCall.setData(false, frozenorunfrozenorderBean);
		try {
			frozenorunfrozenorderCall.callService();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmReceive_001() {
		Logger.start(true, "买家确认收货成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderCallService.dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			// 确认收货请求
			confirmreceivebean.setBuyerId(userId);
			confirmreceivebean.setOrderId(orderId);
			confirmreceivebean.setRemark("收到了啊");
			// 发送请求
			confirmreceivecall.setData(confirmreceivebean);
			confirmreceivecall.callService();
			Logger.verifyEquals(ECode.SUCCESS, confirmreceivecall.getCode(),
					"验证Code");
			// 数据库验证
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(4, database.getTradingStatus(orderId),
					"验证订单交易状态");
			Logger.verifyEquals(
					1,
					database.selectOrderTradingStatusByOrderIdAndTradingStatus(
							orderId, 4).size(), "验证TradingStatus表中 增加一条订单操作记录");

			// java 重构后，确认收货后，异步调用结算服务，实现确认收货结算

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmReceive_002() {
		Logger.start(false, "买家重复确认收货");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			// 确认收货请求
			confirmreceivebean.setBuyerId(userId);
			confirmreceivebean.setOrderId(orderId);
			confirmreceivebean.setRemark("收到了啊");
			// 发送请求
			confirmreceivecall.setData(confirmreceivebean);
			confirmreceivecall.callService();
			Logger.comment("重复请求");
			confirmreceivecall.callService();
			Logger.verifyEquals(ECode.SUCCESS, confirmreceivecall.getCode(),
					"验证Code");
			// 数据库验证
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(4, database.getTradingStatus(orderId),
					"验证订单交易状态");
			Logger.verifyEquals(
					1,
					database.selectOrderTradingStatusByOrderIdAndTradingStatus(
							orderId, 4).size(), "验证TradingStatus表中 增加一条订单操作记录");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmReceive_003() {
		Logger.start(false, "用户不是订单所有者");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			// 确认收货请求
			confirmreceivebean.setBuyerId(userId + 1);
			confirmreceivebean.setOrderId(orderId);
			confirmreceivebean.setRemark("收到了啊");
			// 发送请求
			confirmreceivecall.setData(confirmreceivebean);
			confirmreceivecall.callService();
			Logger.verifyEquals(ECode.FAILED201, confirmreceivecall.getCode(),
					"验证Code");

			Logger.verifyEquals(
					String.format("买家%s没有操作订单%s (%s)的权限",
							confirmreceivebean.getBuyerId(), orderId, userId),
					confirmreceivecall.getMsg(), "验证msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmReceive_004() {
		Logger.start(false, "用户id不存在");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			// 确认收货请求
			confirmreceivebean.setBuyerId(1);
			confirmreceivebean.setOrderId(orderId);
			confirmreceivebean.setRemark("收到了啊");
			// 发送请求
			confirmreceivecall.setData(confirmreceivebean);
			confirmreceivecall.callService();
			Logger.verifyEquals(ECode.FAILED201, confirmreceivecall.getCode(),
					"验证Code");

			Logger.verifyEquals(
					String.format("买家%s没有操作订单%s (%s)的权限",
							confirmreceivebean.getBuyerId(), orderId, userId),
					confirmreceivecall.getMsg(), "验证msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmReceive_005() {
		Logger.start(false, "用户id为null");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			// 确认收货请求
			// confirmreceivebean.setBuyerId(1);
			confirmreceivebean.setOrderId(orderId);
			confirmreceivebean.setRemark("收到了啊");
			// 发送请求
			confirmreceivecall.setData(confirmreceivebean);
			confirmreceivecall.callService();
			Logger.verifyEquals(ECode.FAILED201, confirmreceivecall.getCode(),
					"验证Code");

			Logger.verifyEquals(
					String.format("买家%s没有操作订单%s (%s)的权限", 0, orderId, userId),
					confirmreceivecall.getMsg(), "验证msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmReceive_006() {
		Logger.start(false, "订单号不存在");
		try {
			Integer orderId = 1111;
			confirmreceivebean.setBuyerId(1113);
			confirmreceivebean.setOrderId(orderId);
			confirmreceivebean.setRemark("收到了啊");
			// 发送请求
			confirmreceivecall.setData(confirmreceivebean);
			confirmreceivecall.callService();
			Logger.verifyEquals(ECode.FAILED201, confirmreceivecall.getCode(),
					"验证Code");
			Logger.verifyEquals("订单不存在:" + confirmreceivebean.getOrderId(),
					confirmreceivecall.getString("msg"), "验证接口返回消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmReceive_007() {
		Logger.start(false, "订单号为null");
		try {
			int orderId = 1111;
			confirmreceivebean.setBuyerId(1113);
			// confirmreceivebean.setOrderId(orderId);
			confirmreceivebean.setRemark("收到了啊");
			// 发送请求
			confirmreceivecall.setData(confirmreceivebean);
			confirmreceivecall.callService();
			Logger.verifyEquals(ECode.ERROR400, confirmreceivecall.getCode(),
					"验证Code");
			Logger.verifyEquals("orderId:订单号未提供",
					confirmreceivecall.getString("msg"), "验证接口返回消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmReceive_009() {
		Logger.start(true, "买家确认收货成功后，可以评价订单");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderCallService.dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			// 确认收货请求
			confirmreceivebean.setBuyerId(userId);
			confirmreceivebean.setOrderId(orderId);
			confirmreceivebean.setRemark("收到了啊");
			// 发送请求
			confirmreceivecall.setData(confirmreceivebean);
			confirmreceivecall.callService();
			Logger.verifyEquals(ECode.SUCCESS, confirmreceivecall.getCode(),
					"验证Code");
			// 数据库验证
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(4, database.getTradingStatus(orderId),
					"验证订单交易状态");
			Logger.verifyEquals(
					1,
					database.selectOrderTradingStatusByOrderIdAndTradingStatus(
							orderId, 4).size(), "验证TradingStatus表中 增加一条订单操作记录");

			// java 重构后，确认收货后，异步调用结算服务，实现确认收货结算

			// 订单主表
			List<Map> ymtOrders = database.selectOrderByOderId(orderId);
			Logger.verifyEquals(true,
					(Boolean) ymtOrders.get(0).get("bCanEvaluate"),
					"验证订单确认收货后可以评价");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmReceive_010() {
		Logger.start(true, "订单在退款退货中，确认收货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderCallService.dispatched(orderId, sellerId);

			// 退货退款同步
			orderCallService.applySalesRefundRequestInJava(orderId);
			Logger.comment("前置数据准备结束");
			// 确认收货请求
			confirmreceivebean.setBuyerId(userId);
			confirmreceivebean.setOrderId(orderId);
			confirmreceivebean.setRemark("收到了啊");
			// 发送请求
			confirmreceivecall.setData(confirmreceivebean);
			confirmreceivecall.callService();
			Logger.verifyEquals(ECode.FAILED201, confirmreceivecall.getCode(),
					"验证Code");
			Logger.verifyEquals(ECode.ErrStatusNotMatch,
					confirmreceivecall.getBCode(), "验证bcode");
			Logger.verifyEquals("哈尼，关于该订单，您有未完成的退单,请完成退单流程或者撤销退单再确认收货哦~",
					confirmreceivecall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ConfirmReceive_011() {
		Logger.start(true, "订单锁定中，确认收货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 支付订单
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderCallService.dispatched(orderId, sellerId);

			orderCallService.markOrderLock(orderId, 1);

			Logger.comment("前置数据准备结束");
			// 确认收货请求
			confirmreceivebean.setBuyerId(userId);
			confirmreceivebean.setOrderId(orderId);
			confirmreceivebean.setRemark("收到了啊");
			// 发送请求
			confirmreceivecall.setData(confirmreceivebean);
			confirmreceivecall.callService();
			Logger.verifyEquals(ECode.FAILED201, confirmreceivecall.getCode(),
					"验证Code");
			Logger.verifyEquals(ECode.ErrStatusNotMatch,
					confirmreceivecall.getBCode(), "验证bcode");
			Logger.verifyEquals("该订单已被洋码头客服锁定，暂时无法对该订单进行后续操作",
					confirmreceivecall.getMsg(), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
